package Simulation;

import mathwork.Mathwork;
import fxana.FxRawDataSet;
import fxana.RawDataCell;

public class SimulatorRLSMDouble extends Simulator {
	public static int opt_numberofday=35;
	public static int opt_losscutval=33;
	public static int opt_ref_numberofday=30;
	public static int opt_limit_numberofday=36;

	public SimulatorRLSMDouble(){
		super();
	}
	
	@Override
	protected void ParameterOptimize(FxRawDataSet dataset,
			EvaluatorParaFlag pflag) {
		int n=0,on=0;
		int l=0,ol=0;
		int r=0,or=0;
		int d=0,od=0;
		Double MaxMerit=0.0;
		
		for(r=5;r<35;r+=5)
			for (d=20;d<40;d+=1)
				for(n=20;n<40;n+=1)
					for(l=5;l<35;l+=5){
						EvaluatorParaFlag  spflag=new EvaluatorParaFlag ();
						spflag.SetMFlag(n);
						spflag.SetLFlag(l);
						spflag.SetDFlag(d);
						spflag.SetRFlag(r);
				
						ConditionRLSMDouble seed=new ConditionRLSMDouble();
						SimulationSystem eval=new SimulationSystem(dataset,spflag,seed);
						eval.output();
						if(eval.GetTotalMerit().compareTo(MaxMerit)>0){
							on=n;
							ol=l;
							od=d;
							or=r;
							MaxMerit=eval.GetTotalMerit();
						}
						System.out.println("Temp MaxMerit:"+Mathwork.Roundup(MaxMerit,2)+" "
											+ "N:"+on+" "
											+ "L:"+ol+" "
											+ "d:"+od+" "
											+  "r:"+or);
					}
			
		
		System.out.println("MaxMerit:"+Mathwork.Roundup(MaxMerit,2)+" "+ "is following:");
		EvaluatorParaFlag  spflag=new EvaluatorParaFlag ();
		spflag.SetMFlag(on);
		spflag.SetLFlag(ol);
		spflag.SetRFlag(or);
		spflag.SetDFlag(od);
		
		Condition seed;
		seed=new ConditionRLSMDouble();
		SimulationSystem eval=new SimulationSystem(dataset,spflag,seed);
		eval.output();
	}

	@Override
	protected void OneTest(FxRawDataSet dataset, EvaluatorParaFlag pflag) {
		if(!pflag.IsMFlagSet()){
			pflag.SetMFlag(opt_numberofday);
		}
		
		if(!pflag.IsDFlagSet()){
			pflag.SetDFlag(opt_ref_numberofday);
		}
		
		if(!pflag.IsLFlagSet()){
			pflag.SetLFlag(opt_losscutval);
		}
		
		if(!pflag.IsRFlagSet()){
			pflag.SetRFlag(opt_limit_numberofday);
		}
		
		ConditionRLSMDouble seed=new ConditionRLSMDouble();
		SimulationSystem eval=new SimulationSystem(dataset,pflag,seed);
		eval.output();
	}

	@Override
	protected void OutParameter(FxRawDataSet dataset, EvaluatorParaFlag pflag) {
		RawDataCell today=dataset.GetRawDataBuffer().lastElement();
		Double sellval=0.0;
		Double buyval=0.0;
		Double buylosscut=0.0;
		Double selllostcut=0.0;
		
		ConditionRLSMDouble seed=null;
		EvaluatorParaFlag  spflag=new EvaluatorParaFlag ();
		
		seed=new ConditionRLSMDouble();
		spflag.SetMFlag(SimulatorRLSMDouble.opt_numberofday);
		spflag.SetLFlag(SimulatorRLSMDouble.opt_losscutval);
		spflag.SetDFlag(SimulatorRLSMDouble.opt_ref_numberofday);
		spflag.SetRFlag(SimulatorRLSMDouble.opt_limit_numberofday);
		
		//Mathwork.ifplot=true;
		RawDataCell est=seed.GetEstimatedValue(dataset, today,spflag);
		//Mathwork.ifplot=false;
		if(est!=null){
			sellval=Mathwork.Roundup(est.GetHighValue(),2);
			selllostcut=Mathwork.Roundup(sellval+SimulatorRLSMDouble.opt_losscutval/10.0,2);
			buyval=Mathwork.Roundup(est.GetLowValue(),2);
			buylosscut=Mathwork.Roundup(buyval-SimulatorRLSMDouble.opt_losscutval/10.0,2);
		}else{
			System.out.println("Today can not be estimated!");
			sellval=selllostcut=buyval=buylosscut=0.0;
		}
		
		
		System.out.println("1.today");
		today.out();
		System.out.println(today.GetFSRecDate()+" "+"Sellval: "+sellval+" "+"Buyval: "+buyval+" "+"SellLosscut: " + selllostcut+" "+"Buylosscut: "+buylosscut);
		
		System.out.println("\n2.Waiting");
		SimulationSystem eval=new SimulationSystem(dataset,spflag,seed);
		eval.OutputWorking();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		SimulatorRLSMDouble sim=new SimulatorRLSMDouble();
		sim.DoSimulator(args);
	}

}
